package cn.personal.study.web.rest;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
 * TODO 〈一句话功能简述〉
 *
 * @author duliurui
 * @since 1.2.0
 */
@ControllerAdvice
public class ExceptionAdvice {

    @ExceptionHandler(IOException.class)
    public void ioException(Exception e, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("exception", e);
        // 定向到系统异常展示页面
        RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
        request.setAttribute("errorMsg", "IO异常");
        rd.forward(request, response);
    }

    @ExceptionHandler(RuntimeException.class)
    public void runtimeException(Exception e, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("exception", e);
        // 定向到系统异常展示页面
        RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
        request.setAttribute("errorMsg", "运行时异常");
        rd.forward(request, response);
    }
}
